﻿using System;
using System.Collections.Generic;
using System.Text;
using ChromiumUpdater.Data;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace ChromiumUpdater
{
    public class Logger
    {
        private static string executingAssemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        private static string date = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString();
        private static string logPath = executingAssemblyPath + @"\Logs\Log-" + date + ".txt";
        

        public static void Log(string log, ConfigSettings config)
        {
            if (!File.Exists(logPath))
            {
                using (StreamWriter writer = File.CreateText(logPath))
                {
                    writer.WriteLine("Chromium Updater Error Log");
                    writer.WriteLine("Application Version: " + Assembly.GetExecutingAssembly().GetName().Version);
                    writer.WriteLine("Operating System: " + System.Environment.OSVersion.ToString());
                }
            }

            using (StreamWriter writer = File.AppendText(logPath))
            {
                writer.WriteLine("#######################################################################");
                writer.WriteLine(DateTime.Now + " : " + log);
                writer.WriteLine("-----------------------------------------------------------------------");
                writer.WriteLine("Config Setting: CurrentVersion: " + config.currentVersion.ToString());
                writer.WriteLine("Config Setting: ChromePath: " + config.chromePath.ToString());
                writer.WriteLine("Config Setting: ProxyEnabled: " + config.proxyEnabled.ToString());
                writer.WriteLine("Config Setting: ProxyUrl: " + config.proxyURL.ToString());
                writer.WriteLine("Config Setting: ProxyPort: " + config.proxyPort.ToString());
                writer.WriteLine("Config Setting: AutoUpdateHours: " + config.autoUpdateHours.ToString());
                writer.WriteLine("Config Setting: AutoUpdateMode: " + config.autoUpdateMode.ToString());
            }
            //writer.Close();
            
        }
        public static void Log(string log, ConfigSettings config, Exception ex)
        {

            if (!File.Exists(logPath))
            {
                using (StreamWriter writer = File.CreateText(logPath))
                {
                    writer.WriteLine("Chromium Updater Error Log");
                    writer.WriteLine("Application Version: " + Assembly.GetExecutingAssembly().GetName().Version);
                    writer.WriteLine("Operating System: " + System.Environment.OSVersion.ToString());
                }
            }
            using (StreamWriter writer = File.AppendText(logPath))
            {
                writer.WriteLine("#######################################################################");
                writer.WriteLine(DateTime.Now + " : " + log);
                writer.WriteLine("-----------------------------------------------------------------------");
                writer.WriteLine("Config Setting: CurrentVersion: " + config.currentVersion.ToString());
                writer.WriteLine("Config Setting: ChromePath: " + config.chromePath.ToString());
                writer.WriteLine("Config Setting: ProxyEnabled: " + config.proxyEnabled.ToString());
                writer.WriteLine("Config Setting: ProxyUrl: " + config.proxyURL.ToString());
                writer.WriteLine("Config Setting: ProxyPort: " + config.proxyPort.ToString());
                writer.WriteLine("Config Setting: AutoUpdateHours: " + config.autoUpdateHours.ToString());
                writer.WriteLine("Config Setting: AutoUpdateMode: " + config.autoUpdateMode.ToString());
                writer.WriteLine("-----------------------------------------------------------------------");
                writer.WriteLine("Exception: Message: " + ex.Message.ToString());
                writer.WriteLine("Exception: Source: " + ex.Source.ToString());
                writer.WriteLine("Exception: Data: " + ex.Data.ToString());
                writer.WriteLine("Exception: Stack Trace: " + ex.StackTrace.ToString());
                writer.WriteLine(GetInnerException(ex));
            }
        }

        private static string GetInnerException(Exception ex)
        {
            if (ex.InnerException != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Inner Exception: Message: " + ex.InnerException.Message.ToString());
                sb.AppendLine("Inner Exception: Source: " + ex.InnerException.Source.ToString());
                sb.AppendLine("Inner Exception: Data: " + ex.InnerException.Data.ToString());
                sb.AppendLine("Inner Exception: Stack Trace: " + ex.InnerException.StackTrace.ToString());
                sb.AppendLine(GetInnerException(ex.InnerException));
                return sb.ToString();
            }
            return string.Empty;
        }

    }
}
